package com.F03.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.F02.actionform.F020106ActionForm;
import com.F02.dao.F020106DAO;
import com.F03.actionform.F0301ActionForm;
import com.F03.dao.PdPlanningDAO;
import com.F03.info.PdPlanningInfo;
import com.F05.actionform.F0503ActionForm;
import com.F05.dao.FInvoicingProductionDAO;
import com.action.BaseAction;
import com.config.Constants;
import com.exception.SelectException;
import com.info.T_S_YHCZXXinfo;
import com.util.CommonTools;


public class F0301Action  extends BaseAction{

	/** 记录操作日志 */
	Logger log = Logger.getLogger(F0301Action.class);

	/** 操作记录INFO */
	T_S_YHCZXXinfo t_YhczxxInfo = null;

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		/** 获得actionForm对象 */
		F0301ActionForm actionForm = (F0301ActionForm) form;
		String operationType = actionForm.getOperationType();
		PdPlanningDAO dao = new PdPlanningDAO();		
		try {
			/** 生成操作记录语句 */
			t_YhczxxInfo = setYhczxx(request, response, operationType,"F0301");
			/**
			 * @页面初始�?
			 * @统一参数：operationType=init
			 * @规范：左树进入连接页面不执行查询�?
			 */
			if (Constants.INIT.equals(operationType)){
				return mapping.findForward(Constants.List);
			}
			/**
			 * @查询功能
			 * @统一参数：operationType=queryList
			 * @规范：查询后回显查询条件。如果查询结果无记录，在页面给出提示�?
			 */
			if (Constants.QUERYLIST.equals(operationType)) {
				actionForm.setList(dao.queryList(actionForm));
				return mapping.findForward(Constants.List);
			}
			/**
			 * @新增功能
			 * @统一参数：operationType=add
			 * @规范：进入新增页面�??
			 */
			if (Constants.ADD.equals(operationType)){
				doADD(actionForm, dao);
				return mapping.findForward(Constants.detail);
			}
			/**
			 * @新增页面AJAX功能
			 * @统一参数：operationType=query
			 * @规范：在本页面动态加载值

			 */
			if (Constants.AJAX.equals(operationType)) {
				initAjax(dao,request,response);
				return null;
			}
			
			/**
			 * @编辑功能
			 * @统一参数：operationType=edit
			 * @规范：进入修改页面�??
			 */
			if (Constants.EDIT.equals(operationType)){
				doEdit(actionForm,dao);
				return mapping.findForward("edit");
			}
			/**
			 * @编辑保存功能
			 * @统一参数：operationType=update
			 * @规范：修改保存后，返回查询页面，更新记录，给出用户提示信息�??
			 */
			if (Constants.SAVE_UPDATE.equals(operationType)){
				if(doUpdate(actionForm,dao)){				
					String url=mapping.findForward("doList").getPath();
		            ActionForward forward=new ActionForward(url+"?operationType=queryList&errorFlag="+actionForm.getErrorFlag());
		    		return forward;
				}else{
					return mapping.findForward("detail");		
				}
			}
			/**
			 * @新增保存功能
			 * @统一参数：operationType=insert
			 * @规范：新增保存后，返回查询页面，更新记录，给出用户提示信息�?��??
			 */
			if (Constants.SAVE_INSERT.equals(operationType)){
				doSAVE_INSERT(actionForm, dao, request);
				String url=mapping.findForward("doList").getPath();
	            ActionForward forward=new ActionForward(url+"?operationType=queryList&errorFlag="+actionForm.getErrorFlag());
	    		return forward;
				//return mapping.findForward(Constants.List);
			}
			/**
			 * @删除功能
			 * @统一参数：operationType=delete
			 * @规范：更新记录，给出用户提示信息�?
			 */
			if (Constants.DELETE.equals(operationType)) {				
				if(doDelete(actionForm,dao)){				
					String url=mapping.findForward("doList").getPath();
		            ActionForward forward=new ActionForward(url+"?operationType=queryList");
		    		return forward;
				}else{
					return mapping.findForward("list");		
				}
			}
			/**
			 * @返回List页面
			 * @统一参数：operationType=back
			 * @规范：返回LIST页面，显示查询结果


			 */
			if (Constants.BACK.equals(operationType)) {
				return mapping.findForward(Constants.List);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (dao != null)
				dao.disConnect();
		}
		return mapping.findForward(Constants.INIT);
	}

	/**
	 * @初始�?
	 */
	public void doINIT(F020106ActionForm actionForm,F020106DAO dao) throws SelectException {		
		// 模型类别
		actionForm.setModelTypeList(dao.List_t_product_model());
		actionForm.setCurrentPage(1);
	}

	/**
	 * @新增
	 */
	private void doADD(F0301ActionForm actionForm, PdPlanningDAO dao){		
		//清空输入值		
		actionForm.setID("");
		actionForm.setManufacturingOrder("");
		actionForm.setModelName("");
		actionForm.setFamilyCode("");
		actionForm.setQuantity("");
		actionForm.setGroups("");
		actionForm.setGroupQty("");
		actionForm.setStartDate("");
		actionForm.setFinishDate("");
		actionForm.setPurchaseFlag("");
		actionForm.setDescription("");
		actionForm.setOperationType(Constants.SAVE_INSERT);	
	
	}
	
	private boolean doDelete(F0301ActionForm actionForm,PdPlanningDAO dao){
		boolean flag=false;
		try {
			String ID=CommonTools.null2String(actionForm.getID());
			flag=dao.delete(ID, actionForm);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	private void  doEdit(F0301ActionForm actionForm,PdPlanningDAO dao) throws SelectException{
		try {
			
		    Map map = null;
		    List list = new ArrayList();
			list = dao.selectByIdOther(actionForm.getID());
			 for(int i = 0; i < list.size();i++){
				map = (Map)list.get(i);	
				actionForm.setID(CommonTools.null2String((String)map.get("ID")));
				actionForm.setManufacturingOrder(CommonTools.null2String((String)map.get("MANUFACTURINGORDER")));
				actionForm.setGroups(CommonTools.null2String((String)map.get("GROUPS")));
				actionForm.setGroupQty(CommonTools.null2String((String)map.get("GROUPQTY")));
				actionForm.setPurchaseFlag(CommonTools.null2String((String)map.get("PURCHASEFLAG")));
				actionForm.setAllQty(CommonTools.null2String((String)map.get("ALLQTY")));
				actionForm.setQuantity(CommonTools.null2String((String)map.get("QUANTITY")));
				actionForm.setFinishDate(CommonTools.null2String((String)map.get("FINISHDATE")));
				actionForm.setStartDate(CommonTools.null2String((String)map.get("STARTDATE")));
				actionForm.setFamilyCode(CommonTools.null2String((String)map.get("FAMILYCODE")));
				actionForm.setDescription(CommonTools.null2String((String)map.get("DESCRIPTION")));
				actionForm.setModel(CommonTools.null2String((String)map.get("MODEL")));
				actionForm.setLayOutType("");
				actionForm.setGroupQtyOther(CommonTools.null2String((String)map.get("GROUPQTY")));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
	
	/**
	 * @新增保存
	 * @执行新增后，清空actionForm，执行查询操作，返回查询界面。


	 */
	private void doSAVE_INSERT(F0301ActionForm actionForm, PdPlanningDAO dao,
			HttpServletRequest request) throws SelectException {
		String keystr = request.getParameter("keystr");			
		String[] groups = request.getParameterValues("crow");
		String[] groupQty = request.getParameterValues("frow");
		String[] startDate = request.getParameterValues("prow");
		String[] finishDate = request.getParameterValues("brow");
		String[] description = request.getParameterValues("mrow");
		Map map = new HashMap();
		if (keystr != null && !"".equals(keystr)) {
			String[] idArray = keystr.split(",");
			for (int i = 0; i < idArray.length; i++) {
				map.put("1"+i, groups[i]);
				map.put("2"+i, groupQty[i]);
				map.put("3"+i, startDate[i]);
				map.put("4"+i, finishDate[i]);
				map.put("5"+i, description[i]);							
			}
				dao.SAVE_INSERT(actionForm,map,idArray.length);
		}		
	}


	/**
	 * @编辑保存
	 */
	private boolean doUpdate(F0301ActionForm actionForm,PdPlanningDAO dao) throws SelectException{
		boolean flag=false;
		try {
			actionForm.setErrorFlag("");
			flag=dao.update(actionForm);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	/**
	 * @throws SelectException
	 * @加载t_critical_phases的code,name值

	 * @执行页面动态加载

	 */
	private void initAjax(PdPlanningDAO dao,HttpServletRequest request, HttpServletResponse response){
				
		try{
			response.setContentType("text/xml");
			response.setCharacterEncoding("UTF-8");
			String id = CommonTools.trim(request.getParameter("id"));
			String responseXML = "";
			if(id.equals("modelquantity")){
				String manufacturingOrder = CommonTools.trim(request.getParameter("manufacturingOrder"));
				responseXML = dao.getModelQuantity(manufacturingOrder);	
			}
			else if(id.equals("beginDate")){
				String manufacturingOrder = CommonTools.trim(request.getParameter("manufacturingOrder"));
				String finishDate = CommonTools.trim(request.getParameter("finishDate"));
				responseXML = dao.getBeginDate(manufacturingOrder, finishDate);
			}

			log.info(responseXML.toString());
			response.getWriter().write(responseXML.toString());
									
		}catch (Exception e){
			e.printStackTrace();
		}
	}
	
	
}
